home *** CD-ROM | disk | FTP | other *** search
-
- #include "main.h"
- #include "BlitCmp.h"
-
- Boolean gQuit;
- Rect gSrcRect, gDstRect;
- GWorldPtr gSrcWorld = nil;
- WindowPtr gTheWindow;
-
- #ifdef powerc
- QDGlobals qd;
- #endif
-
-
-
- main()
- {
- MaxApplZone(); /* Expand the heap so code segments load
- at the top */
- InitToolbox(); /* Initialize the program */
- MainEventLoop(); /* Call the main event loop */
- }
-
-
- void
- InitToolbox()
- {
- Handle menuBar;
- EventRecord event;
- short count;
-
- gQuit = FALSE;
-
- InitGraf((Ptr) &qd.thePort);
- InitWindows();
- InitMenus();
- InitDialogs(nil);
- InitCursor();
-
- menuBar = GetNewMBar(kMenuBarId); /* Read menus into menu bar */
- if ( menuBar != nil )
- {
- SetMenuBar(menuBar); /* Install menus */
- DisposHandle(menuBar);
-
- AddResMenu(GetMHandle(mApple), 'DRVR'); /* Add DA names to Apple menu */
- DrawMenuBar();
- }
- else
- DebugStr("\pCould not get menubar");
-
- DoNewWindow();
-
- if ((gSrcWorld = GetPictWorld(&gSrcRect, 0, 128)) == nil)
- {
- // DebugStr("\pCould not get the World");
- return;
- }
-
- gDstRect = gSrcRect;
-
- /* make sure the left of the dst rect is below 200 so we don't overwrite the times */
- if ( gDstRect.left < 200 )
- {
- OffsetRect(& gDstRect, 200, 0 );
- }
- }
-
-
- void
- MainEventLoop()
- {
- RgnHandle cursorRgn;
- EventRecord event;
- Point mouse;
-
- cursorRgn = nil;
- while ( ! gQuit )
- {
- if ( WaitNextEvent(everyEvent, &event, MAXLONG, cursorRgn) )
- DoEvent(&event);
- }
- }
-
-
- void
- DoEvent(EventRecord *event)
- {
- switch ( event->what )
- {
- case mouseDown:
- DoMouseDown(event);
- break;
-
- case keyDown:
- case autoKey:
- DoKeyPress(event);
- break;
-
- case updateEvt:
- DoUpdate(event);
- break;
- }
- }
-
-
- void
- DoKeyPress(EventRecord *event)
- {
- char key;
-
- key = event->message & charCodeMask;
- if ( event->modifiers & cmdKey ) /* Command key down? */
- {
- DoMenuCommand(MenuKey(key));
- }
- }
-
-
- void
- DoMouseDown(EventRecord *event)
- {
- long newSize;
- Rect growRect;
- WindowPtr theWindow;
- short part = FindWindow(event->where, &theWindow);
-
- switch ( part )
- {
- case inMenuBar: /* Process a mouse menu command (if any) */
- DoMenuCommand(MenuSelect(event->where));
- break;
-
- case inSysWindow: /* Let the system handle the mouseDown */
- SystemClick(event, theWindow);
- break;
-
- case inContent:
- if ( theWindow != FrontWindow() )
- SelectWindow(theWindow);
- else
- DoContentClick(event, theWindow);
- break;
-
- case inDrag: /* Pass screenBits.bounds to get all gDevices */
- DragWindow(theWindow, event->where, &qd.screenBits.bounds);
- break;
-
- case inGrow:
- growRect = qd.screenBits.bounds;
- growRect.top = growRect.left = 80; /* Arbitrary minimum size. */
- newSize = GrowWindow(theWindow, event->where, &growRect);
- if (newSize != 0)
- {
- InvalidateScrollbars(theWindow);
- SizeWindow(theWindow, LoWord(newSize), HiWord(newSize), TRUE);
- InvalidateScrollbars(theWindow);
- }
- break;
-
- case inGoAway:
- if (TrackGoAway(theWindow, event->where))
- {
- CloseAnyWindow(theWindow);
- gQuit = true;
- }
- break;
- }
- }
-
-
- void
- DoUpdate(EventRecord *event)
- {
- WindowPtr theWindow = (WindowPtr) event->message;
-
- if ( IsAppWindow(theWindow) )
- {
- BeginUpdate(theWindow); /* This sets up the visRgn */
- if (! EmptyRgn(theWindow->visRgn))
- { /* Draw if updating needs to be done */
- SetPort(theWindow);
- EraseRgn(theWindow->visRgn);
- DoUpdateWindow(event);
- DrawGrowIcon(theWindow);
- }
- EndUpdate(theWindow);
- }
- }
-
- void
- DoMenuCommand(long menuResult)
- {
- short menuID; /* The resource ID of the selected menu */
- short menuItem; /* The item number of the selected menu */
- Str255 daName;
- FSSpec theFile;
-
- menuID = HiWord(menuResult);
- menuItem = LoWord(menuResult);
- switch ( menuID )
- {
- case mApple:
- switch ( menuItem )
- {
- case iAbout:
- (void) Alert(kAboutAlertId, nil);
- break;
- default: /* All non-About items in this menu are DAs */
- GetItem(GetMHandle(mApple), menuItem, daName);
- (void) OpenDeskAcc(daName);
- break;
- }
- break;
-
- case mFile:
- switch ( menuItem )
- {
- case iQuit:
- gQuit = true;
- break;
- }
- break;
-
- case mEdit:
- switch (menuItem)
- {
- /* Call SystemEdit for DA editing & MultiFinder */
- /* since we don’t do any Editing */
- case iUndo:
- case iCut:
- case iCopy:
- case iPaste:
- case iClear:
- (void) SystemEdit(menuItem - 1);
- break;
- }
- break;
- }
- HiliteMenu(0); /* Unhighlight what MenuSelect or MenuKey hilited */
- }
-
- void
- CloseAnyWindow(WindowPtr window)
- {
- if (IsDAWindow(window))
- {
- CloseDeskAcc( ( (WindowPeek) window )->windowKind );
- } else if (IsDialogWindow(window))
- {
- HideWindow(window);
- }
- else if (IsAppWindow(window))
- {
- CloseWindow(window);
- }
- }
-
- Boolean
- IsAppWindow(WindowPtr window)
- {
- short windowKind;
-
- if ( window == nil )
- return false;
- else
- {
- windowKind = ((WindowPeek) window)->windowKind;
- return ((windowKind >= userKind) || (windowKind == dialogKind));
- }
- }
-
- Boolean
- IsDAWindow(WindowPtr window)
- {
- if ( window == nil )
- return false;
- else
- return ( ((WindowPeek) window)->windowKind < 0 );
- }
-
- Boolean
- IsDialogWindow(WindowPtr window)
- {
- if ( window == nil )
- return false;
- else
- return ( ((WindowPeek) window)->windowKind == dialogKind );
- }
-
- void
- DoNewWindow(void)
- {
- WindowPtr newWindow;
- Rect bounds;
-
- SetRect(&bounds, 0, 40, 620, 480);
- gTheWindow = NewCWindow(nil, &bounds, "\pBlitCmp", false, documentProc,
- (WindowPtr)(-1), true, 0L);
-
- if (gTheWindow == nil)
- return;
-
- ((WindowPeek)gTheWindow)->windowKind = userKind;
-
- ShowWindow(gTheWindow);
- SetPort(gTheWindow);
- }
-
-
- void
- DoContentClick(EventRecord *event, WindowPtr win)
- {
- Point pt;
-
- SetPort((GrafPtr)win);
- pt = event->where;
- LocalToGlobal(&pt);
-
- InvalRect(&win->portRect);
- }
-
- void
- DoUpdateWindow(EventRecord *event)
- {
- BlitProcPtr theWinner;
- PixMapHandle srcPixHandle;
-
- if (gSrcWorld == nil) // Not yet ready to update
- return;
-
- if ((srcPixHandle = GetGWorldPixMap(gSrcWorld)) == nil)
- {
- // DebugStr("\pCould not fetch the source PixMapHandle");
- return;
- }
-
- HLock((Handle) srcPixHandle);
- LockPixels(srcPixHandle);
-
- theWinner = BestBlitter((BlitProcPtr)MyCopyPixels, srcPixHandle, &gSrcRect, &gDstRect);
-
- UnlockPixels(srcPixHandle);
- HUnlock((Handle) srcPixHandle);
-
- MoveTo(10, 100);
- if (theWinner == (BlitProcPtr) MyCopyPixels)
- DrawString("\pUse the other Blit Proc...");
- else
- DrawString("\pIt's better to use CopyBits()");
- }
-
-
- pascal void MyCopyPixels(BitMapPtr srcBits, BitMapPtr dstBits,
- Rect *srcRect, Rect *dstRect,
- short mode, RgnHandle mask)
- {
- PixMapPtr srcPM;
- PixMapPtr dstPM;
- long dstLeft;
- long dstRight;
- long srcLeft;
- long srcRowBytes;
- long dstRowBytes;
- long * srcRow;
- long * dstRow;
- long * srcPtr;
- long * dstPtr;
- long leftMask;
- long notLeftMask;
- long rightMask;
- long notRightMask;
- long dstLong;
- short width;
- short dstLongs;
- short height;
- long offset;
-
- srcPM = (PixMapPtr) srcBits;
- dstPM = (PixMapPtr) dstBits;
-
- srcRow = (long *) srcPM->baseAddr;
- dstRow = (long *) dstPM->baseAddr;
-
- srcRowBytes = srcPM->rowBytes & 0x3fff;
- dstRowBytes = dstPM->rowBytes & 0x3fff;
-
- // get the bit offset to the src left edge
- srcLeft = srcRect->left - srcPM->bounds.left;
- srcLeft *= srcPM->pixelSize;
-
- // offset the src ptr to the first long
- srcRow += srcLeft >> 5;
-
- // get the bit offset to the dst left and right edges
- dstLeft = dstRect->left - dstPM->bounds.left;
- dstLeft *= dstPM->pixelSize;
- dstRight = dstRect->right - dstPM->bounds.left;
- dstRight *= dstPM->pixelSize;
-
- // get the number of middle longs to do minus the left edge long
- dstLongs = dstRight - dstLeft;
- dstLongs >>= 5;
- dstLongs -= 2;
-
- // offset the dst Ptr to the first long
- dstRow += dstLeft >> 5;
-
- // now compute left and right masks for the dst
- dstLeft &= 0x1f;
- leftMask = ( 1 << dstLeft ) - 1;
- notLeftMask = ~leftMask;
-
- dstRight &= 0x1f;
- notRightMask = ( 1 << dstRight ) - 1;
- rightMask = ~notRightMask;
-
- // offset the src and dst ptrs to the first row
- offset = srcRect->top - srcPM->bounds.top;
- offset *= srcRowBytes;
- srcRow += (long *) offset;
-
- offset = dstRect->top - dstPM->bounds.top;
- offset *= dstRowBytes;
- dstRow += (long *) offset;
-
- height = dstRect->bottom - dstRect->top - 1;
-
- /* check if we need to do the left and right mask */
- if ( leftMask )
- {
- if ( notLeftMask == 0 )
- {
- leftMask = 0;
- dstLongs++;
- }
- }
-
- if ( rightMask )
- {
- if ( notRightMask == 0 )
- {
- rightMask = 0;
- dstLongs++;
- }
- }
-
- for ( ; height >= 0; --height )
- {
- srcPtr = srcRow;
- dstPtr = dstRow;
-
- /* do the masked left edge */
- if ( leftMask )
- {
- dstLong = *srcPtr++ & leftMask;
- dstLong |= *dstPtr & notLeftMask;
- *dstPtr++ = dstLong;
- }
-
- /* do the middle longs */
- for ( width = dstLongs; width >= 0; --width )
- {
- *dstPtr++ = *srcPtr++;
- }
-
- /* do the masked right edge */
- if ( rightMask )
- {
- dstLong = *srcPtr & rightMask;
- dstLong |= *dstPtr & notRightMask;
- *dstPtr = dstLong;
- }
-
- /* bump to the next row */
- srcRow += (long *) srcRowBytes;
- dstRow += (long *) dstRowBytes;
- }
- }
-
- GWorldPtr GetPictWorld(Rect *srcRect, short depth, short pictID)
- {
- GWorldPtr srcWorld, oldWorld;
- GDHandle oldGD;
- PicHandle thePict;
- Rect saveSrcRect;
-
- if ((thePict = GetPicture(pictID)) == nil)
- {
- DebugStr("\pError getting the pict");
- return nil;
- }
-
- *srcRect = (**thePict).picFrame; // Make our GWorld the size of the pict
-
- #if 1
- SetRect( srcRect, 200, 0, 600, 400 );
- #endif
-
- // if depth zero, convert our rectangle to global coords to get screen depth and alignment
- if ( depth == 0 )
- {
- saveSrcRect = *srcRect;
- LocalToGlobal( (Point *) srcRect );
- LocalToGlobal( &((Point *) srcRect)[1] );
- }
-
- GetGWorld(&oldWorld, &oldGD);
- if (NewGWorld(&srcWorld, depth, srcRect, nil, nil, useTempMem) == noErr && srcWorld != nil)
- {
- /* make sure the src rect is set to the offscreen's rect */
- *srcRect = srcWorld->portRect;
-
- SetGWorld(srcWorld, nil); // Drawing goes to our new GWorld
- EraseRect(srcRect); // Clear it out
-
- DrawPicture(thePict, srcRect); // Draw our picture
- }
- else
- {
- SysBeep( 5 );
- // DebugStr("\pNewGWorld() failed!");
- srcWorld = nil;
- }
-
- SetGWorld(oldWorld, oldGD);
-
- ReleaseResource((Handle) thePict);
- return srcWorld;
- }
-
-
- void
- InvalidateScrollbars(WindowPtr theWindow)
- {
- Rect tempRect;
-
- SetPort(theWindow);
-
- tempRect = theWindow->portRect;
- tempRect.left = tempRect.right - 15;
- InvalRect(&tempRect);
- EraseRect(&tempRect);
-
- tempRect = theWindow->portRect;
- tempRect.top = tempRect.bottom - 15;
- InvalRect(&tempRect);
- EraseRect(&tempRect);
- }
-